package com.ktwlrj.core.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间处理工具类
 *
 * @author zhouxuanhong
 * @date 2019-04-01 16:55
 * @since JDK1.8
 */
public class KtwlDateUtils {
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYYMMDDHH = "yyyyMMddHH";
    public static final String YYYYMMDDHHMM = "yyyyMMddHHmm";
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String YYYYMMDDHHMMSSSS = "yyyyMMddHHmmssSS";
    public static final String FORMAT_YYYY_MM_DD = "YYYY-MM-dd";
    public static final String FORMAT_YYYY_MM_DD_HH = "YYYY-MM-dd HH";
    public static final String FORMAT_YYYY_MM_DD_HH_MM = "YYYY-MM-dd HH:mm";
    public static final String FORMAT_YYYY_MM_DD_HH_MM_SS = "YYYY-MM-dd HH:mm:ss";
    public static final String FORMAT_YYYY_MM_DD_HH_MM_SS_SS = "YYYY-MM-dd HH:mm:ss:SS";
    private static SimpleDateFormat sdf;


    public static String getStringDate(String format) {
        Date date = new Date();
        return formatDate(date, format);
    }

    public static String getStringDate(String format, Date date) {
        return formatDate(date, format);
    }

    public static String getStringDate(Date date) {
        return formatDate(date, FORMAT_YYYY_MM_DD_HH_MM_SS);
    }


    public static String formatDate(Date date, String format) {
        if (date == null) {
            return "";
        }
        if (StringUtils.isBlank(format)) {
            return "";
        }
        sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }


    public static Date formatDate(String strDate, String format) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        }
        sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static Date formatDate(String strDate) {
        return formatDate(strDate, FORMAT_YYYY_MM_DD_HH_MM_SS);
    }

    public static Long formatLong(Date date) {
        return date.getTime();
    }

    public static Long formatLong(String source, String format) {
        if (StringUtils.isBlank(source)) {
            return null;
        }
        sdf = new SimpleDateFormat(format);
        try {
            Date date = sdf.parse(source);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
}
